<?php

namespace app\models\system;

use Yii;

/**
 * This is the model class for table "set_pay".
 *
 * @property int $id
 * @property string $name
 * @property string $mid 商户ID
 * @property string $secret 密钥32位md5
 * @property string $bid 报备商户编号
 */
class SetPay extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'set_pay';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name', 'mid', 'secret', 'bid'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'Name',
            'mid' => 'Mid',
            'secret' => 'Secret',
            'bid' => 'Bid',
        ];
    }

    public static function getLastOne()
    {
        $model = self::find()
            ->orderBy(['id' => SORT_DESC])
            ->one();

        if (!$model) {
            $model = new self();
        }

        return $model;
    }

    public static function saveData($data)
    {
        $model = self::getLastOne();

        if ($model->load(['SetPay' => $data]) && $model->save()) {
            return true;
        }
    }
}
